//config example 1
//uses the configuration module to initialise allegro and read/write allegro and custom values
#include "../../../axl_config.h"
using namespace AXL_Projects;

int main(void)
{
	int c,r;
	
	Configuration* myConfig;
	std::string config;
	//create a new configuration object reading all values from the xml file
	config="config.xml";
	
	//alternatively you can ignore XML and pass in all the parameters by hand
	// for example, uncomment this:
	/*	
	config="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
	"<config>"
	    "<system fps=\"60\" debugon=\"1\" autowritemain=\"1\" autowritecustom=\"1\" enablejoystick=\"0\" enablekeyboard=\"1\" enablemouse=\"1\" enablesound=\"1\" enablemidi=\"1\" matchrefreshrate=\"0\" />"
	    "<graphics vsync=\"0\" graphicsmode=\"0\" depthpreferred=\"32\" depthfallback=\"16\" />"
	    "<window width=\"800\" height=\"600\" autodetect=\"windowed\" />"
	    "<sound maxvoicearray=\"32\" samplevolume=\"150\" musicvolume=\"128\" />"
	    "<custom name=\"author\" valuestring=\"Buffer Neil\" />"
	    "<custom name=\"badger\" valuestring=\"badger badger badger\" />"
	    "<custom name=\"game\" valuestring=\"turnips from space\" />"
	    "<custom name=\"score\" valueint=\"3\" />"
	    "<custom name=\"gravity\" valuefloat=\"-9.800000\" />"
	"</config>";
	*/
	myConfig=new Configuration(config);
	
	//initialise allegro and leave in a graphics mode, using all the values from the XML (or constructor)
	//0 is success
	//1 is success but some values have changed (e.g. requested windowed, but got fullscreen
	//2 is failure and needs an abort
	r=myConfig->AllegroStart();

	//check for errors
	//will only fail on system or graphics initialisation
	if(r==0 || Configuration::GlobalErrorString!="")
	{
		allegro_message("Initialisation failed. See config.log file\n%s",Configuration::GlobalErrorString.c_str());
		allegro_exit();		
	}
	else
	{
		//simply output everything to the screen
		c=makecol(255,255,255);
		int l=20;
		if(r==1) 
			textprintf_ex(screen,font,0,0,c,-1,"Initialisation ok, but some lesser warnings/errors. see config.log");

		textprintf_ex(screen,font,0,l+0,c,-1,"ok. check config.log has been rewritten (need autowrite on)");
		textprintf_ex(screen,font,0,l+20,c,-1,"Some Stuff. Filename: %s",myConfig->ConfigFile.c_str());
		textprintf_ex(screen,font,0,l+30,c,-1,"fps: %d",myConfig->CapsSystem.fps);
		textprintf_ex(screen,font,0,l+40,c,-1,"sound volume: %d",myConfig->CapsSound.SampleVolume);
		textprintf_ex(screen,font,0,l+50,c,-1,"bpp: %d",myConfig->CapsActualSystem.DepthUsed);
		textprintf_ex(screen,font,0,l+60,c,-1,"mode: %s",myConfig->CapsGraphics.WindowMode.c_str());
		textprintf_ex(screen,font,0,l+70,c,-1,"Custom/author: %s",myConfig->GetCustom("author","not set").c_str());
		textprintf_ex(screen,font,0,l+80,c,-1,"Custom/score: %d (-1 if not there)",myConfig->GetCustom("score",-1));
		textprintf_ex(screen,font,0,l+90,c,-1,"Custom/gravity: %f (-1 if not there)",myConfig->GetCustom("gravity",-1.0F));
		textprintf_ex(screen,font,0,l+100,c,-1,"Custom/newentry: %s (should say 'not set')",myConfig->GetCustom("newentry","not set").c_str());
		textprintf_ex(screen,font,0,l+120,c,-1,"NOW SETTING EXISING AUTHOR and NEW NEWENTRY directly");

		//set some custom values. first updates an existing value, second writes a new entry.
		myConfig->SetCustom("author","New Neil");
		myConfig->SetCustom("newentry","new entry. hurrah");
		textprintf_ex(screen,font,0,l+140,c,-1,"Custom/author: %s",myConfig->GetCustom("author","not set").c_str());
		textprintf_ex(screen,font,0,l+150,c,-1,"Custom/newentry: %s",myConfig->GetCustom("newentry","not set").c_str());
		//relying on the config file to say autowrite on exit, if not need the following:
		//myConfig->FlushCustom();
	}

	readkey();

	//deleting will save config.xml values if the autosave flags are set - which they are in the sample
	//change to see it not save
	delete myConfig;
	return 0;
}
END_OF_MAIN();
